Skip to content

vulkan_replay: enforce index buffer offset alignment in prepare_draw (VK-DRAW-INDEX-OFFSET-ALIGN) - #539

Open
tahffo68hk-hash wants to merge 1 commit into
ProjectLeonida:mainfrom
tahffo68hk-hash:fix/vk-draw-index-offset-align
Open

tahffo68hk-hash wants to merge 1 commit into
ProjectLeonida:mainfrom
tahffo68hk-hash:fix/vk-draw-index-offset-align

Conversation

@tahffo68hk-hash

Copy link
Copy Markdown
Contributor

Summary

detail::prepare_draw() in runtime/src/vulkan_replay.h resolves an indexed draw's draw.index.va via vk.buffer_at(...) into p.index_offset (or falls back to vk.zero_offset when unbound) and later emits vk.bind_index_buffer(command_buffer, d.index_buffer, d.index_offset, d.index_type) without checking alignment of p.index_offset against the index element size.

Vulkan requires offset in vkCmdBindIndexBuffer to be a multiple of the index type's size (2 bytes for VK_INDEX_TYPE_UINT16, 4 bytes for VK_INDEX_TYPE_UINT32; VUID-vkCmdBindIndexBuffer-offset-08783), matching D3D12's IASetIndexBuffer alignment requirement. Passing a misaligned offset previously bypassed prepare_draw() and reached vkCmdBindIndexBuffer directly.

Fix

  • Check p.index_offset % index_align != 0 (index_align = 4 for VK_INDEX_TYPE_UINT32, 2 for VK_INDEX_TYPE_UINT16) in detail::prepare_draw() (runtime/src/vulkan_replay.h), returning ReplayResult::invalid_draw before any Vulkan command is recorded.
  • Add CPU-only replay unit test runtime/test/replay_index_align_test.cpp (registered as replay_index_align in runtime/CMakeLists.txt) covering aligned and misaligned offsets for DXGI_FORMAT_R16_UINT, DXGI_FORMAT_R32_UINT, and unbound zero_buffer fallback, verifying vkCmdBindIndexBuffer is never invoked on rejection.

Verification

  • Built and ran replay_index_align_test.exe and replay_copy_offset_test.exe (MSVC 19.44, Ninja, C++20, CPU-only mock-callback replay tests — not real GPU execution):
    • replay_index_align: 10/10 cases OK
    • replay_copy_offset: CopyResource and CopyTexture apply ResourceBinding::buffer_offset

Coordination

中文翻译

摘要

runtime/src/vulkan_replay.h 中的 detail::prepare_draw() 在解析索引绘制的 draw.index.va(或未绑定时回退至 vk.zero_offset)得到 p.index_offset 后,未校验其与索引元素大小的对齐关系,直接交给 vkCmdBindIndexBuffer。

根据 Vulkan 规范 VUID-vkCmdBindIndexBuffer-offset-08783(与 D3D12 IASetIndexBuffer 要求一致),offset 必须按索引类型大小对齐(VK_INDEX_TYPE_UINT16 为 2 字节,VK_INDEX_TYPE_UINT32 为 4 字节)。此前未对齐的偏移会绕过准备阶段直接触发 Vulkan 验证错误。

修复

  • 在 detail::prepare_draw() 中增加 p.index_offset % index_align 校验(UINT32 为 4、UINT16 为 2),未对齐时返回 ReplayResult::invalid_draw,阻止录制非法 vkCmdBindIndexBuffer。
  • 新增 CPU 单元测试 runtime/test/replay_index_align_test.cpp(注册为 replay_index_align),覆盖 R16_UINT、R32_UINT 及 zero_buffer 回退路径的对齐/未对齐用例,并确认拒绝路径不会调用 vkCmdBindIndexBuffer。

验证

  • 本地编译并运行 replay_index_align_test.exe(10/10 通过)与 replay_copy_offset_test.exe(通过)(均为 CPU 回调测试,非真实 GPU 执行)。

协同

demoz53 added a commit to demoz53/leonida that referenced this pull request Nov 24, 2026
…et alignment in prepare_draw (VK-DRAW-INDEX-OFFSET-ALIGN)
@Sangonapou

Copy link
Copy Markdown
Contributor

Tested 9ef7469 on NVIDIA GeForce RTX 3080 Ti Laptop (Ampere, subgroup 32..32, mesh shaders), NVIDIA 621.24, Vulkan 1.4.357, Windows 11 Pro (build 26300), native Windows: pass

Runtime built at this head with scripts\build_runtime.ps1 (MSVC 19.51, VS Build Tools 2026, Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0). No game files.

ctest -R ^replay_index_align$ -V: replay_index_align: 10/10 cases OK.

@zmasi

zmasi commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

Verification Report (9ef746972e merged into main 59e65849b0)

  • Environment: Windows 11, MSVC 19.44 (Ninja Release), NVIDIA GeForce RTX 3090 (driver 610.88), Vulkan SDK 1.4.363.1. No game data used (check_no_game_data.py rc=0).
  • Diff & Merge Audit: Merged 9ef746972e cleanly into origin/main (59e65849b0, 0 conflicts; 3 files changed: runtime/CMakeLists.txt, runtime/src/vulkan_replay.h, runtime/test/replay_index_align_test.cpp).
    • Checked composition with main's IndirectDrawArgs path in detail::prepare_draw(): for indirect indexed draws (arguments != nullptr), unaligned p.index_offset and null draw.index.va already take indirect_stats::skip(...) (p.skipped = true; return ReplayResult::success;) earlier in if (draw.indexed), while direct indexed draws (arguments == nullptr) previously fell through without checking p.index_offset % index_align on either resolved buffers or vk.zero_offset. Placing the VUID-vkCmdBindIndexBuffer-offset-08783 guard at the end of if (draw.indexed) closes direct-draw alignment without altering indirect-draw soft-skip behavior.

Red -> Green Results

Target Base main vulkan_replay.h + #539 test Merged main + #539 (9ef746972e)
replay_index_align_test (CPU replay) RED (rc=1: misaligned DXGI_FORMAT_R16_UINT offset 1 returns ReplayResult::success and invokes vkCmdBindIndexBuffer) GREEN (rc=0: replay_index_align: 10/10 cases OK across R16_UINT, R32_UINT, and misaligned zero_offset fallback; bind_index_calls == 0 on every rejected draw)
Combined merge with #778 + #1179 + #1443 (d3d12_indirect_draw, d3d12_msaa_draw, replay_index_align on RTX 3090, LEONIDA_VK_VALIDATION=1) — GREEN (rc=0, 0 validation errors)
  • Verdict: PASS (reproduces the misaligned vkCmdBindIndexBuffer bypass on main, passes 10/10 alignment cases, and composes cleanly with main's indirect-draw path).

AI-assisted.

@enlight3d

Copy link
Copy Markdown
Contributor

Validation run of this pull request.

  • PR head: 9ef7469. main: 54b2a81. Merged cleanly (git merge --no-edit --no-ff pr-539, no conflicts); tested at the merge commit.
  • Commands:
    • cmake -S runtime -B runtime/build -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DLEONIDA_REQUIRE_VULKAN=ON
    • cmake --build runtime/build --target gta6rt replay_index_align_test replay_copy_offset_test vk_command_replay_test command_list_test -j 8
    • ctest -R "^(replay_index_align|replay_copy_offset|vk_command_replay|command_list)$" --output-on-failure -j1
  • Green (merged): 4 of 4 passed, none skipped. replay_index_align is a CPU-only mock-callback test (no GPU is involved); the other three share vulkan_replay.h or the command list with the change.
  • Red (behavioural, no API change): runtime/src/vulkan_replay.h taken back from main, test and runtime/CMakeLists.txt kept. The test builds and replay_index_align fails at replay_index_align_test.cpp:88 (replay(list, VK_NULL_HANDLE, no_resolve, vk) == expected: the misaligned offset is not rejected with invalid_draw).
  • Link: no new imports vs main (gta6rt.exe, 714 imports on both).
  • Machine: RTX 3090 24 GB, NVIDIA 621.40, Windows 11 Pro 10.0.26300, MSVC 19.44.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants